<template>
  <div id="login">
    <el-form
      class="login-container"
      label-position="left"
      label-width="0px"
      :model="loginUser"
    >
      <h3 class="login-title" style="text-align: center">用户登录</h3>
      <el-form-item label="">
        <el-input
          type="text"
          v-model="loginUser.username"
          auto-complete="off"
          placeholder="用户名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          type="password"
          v-model="loginUser.password"
          auto-complete="off"
          placeholder="密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item class="button-container" style="width: 100%">
        <el-button-group>
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-edit"
            @click="toRegister"
          >
            注册
          </el-button>
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-share"
            @click="login"
            >提交</el-button
          >
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-delete"
            @click="clearForm"
          >
            清空
          </el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  created() {
    // 先判断本地有没有token
    const jwt_token = localStorage.getItem("jwt_token");
    if (jwt_token) {
      let outer = this;
      // 本地有token，就将token存到全局，方便调用
      this.$store.commit("updateToken", jwt_token);
      // 利用token获取用户信息
      this.$store.dispatch("getInfo", {
        success(resp) {
          // 成功获取信息后，将用户的isLogin标识设置为true
          outer.$store.commit("setIsLogin", true);
          // 跳转到home页面
          outer.$router.push({ name: "home" });
        },
        error(resp) {
          console.log("获取用户信息失败" + resp);
        },
      });
    }
  },
  data() {
    return {
      // 用户登录信息
      loginUser: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    // 清空用户登录信息
    clearForm() {
      this.loginUser = {
        username: "",
        password: "",
      };
    },
    // 登陆成功后调用，获取当前用户信息
    getCurUserInfo() {
      let outer = this;
      this.$store.dispatch("getInfo", {
        success(resp) {
          outer.$store.commit("setIsLogin", true);
          outer.$router.push({ name: "home" });
        },
        error(resp) {
          console.log("获取用户信息失败" + resp);
        },
      });
    },
    // 登陆函数+一些前端数据校验
    login() {
      let outer = this;
      if (this.loginUser.username.length == 0) {
        this.$message.error("用户名不能为空");
      } else if (this.loginUser.password.length == 0) {
        this.$message.error("密码不能为空");
      } else {
        this.$store.dispatch("login", {
          username: outer.loginUser.username,
          password: outer.loginUser.password,
          success(resp) {
            outer.getCurUserInfo();
          },
          error(resp) {
            outer.$message.error("用户名或密码错误");
          },
        });
      }
    },
  },
  mixins: ["toRegister"],
};
</script>

<style scoped>
#login {
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  /* position: fixed; */
  margin: 0px;
  padding: 0px;
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaee;
  box-shadow: 0 0 25px #cac6c6;
}
.login-title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.button-container {
  display: flex;
  justify-content: space-between;
}
</style>
